<?php
class Mdl_blog_frontend extends Model
{
	function __construct()
	{
		parent::Model();		
	}
	
	function Mdl_blog_frontend()
	{
		self::__construct();
	}
	
	function get_posts($limit = NULL)
	{
		if (!empty($limit))
		{
			$this->db->limit($limit);
		}
		
		$this->db->order_by('timestamp', 'desc');
		
		$data = $this->db->get('blog_posts');
		
		if( $data->num_rows() !== 0 )
		{
			return $data->result_array();
		}
		else
		{
			return FALSE;
		}
	}
	
	function get_comments($id){
		$this->db->where('postid', $id);
		$this->db->order_by('timestamp', 'desc');
		
		
		$data = $this->db->get('blog_comments');
		
		if( $data->num_rows() !== 0 ){
			return $data->result_array();
		} else {
			return FALSE;
		}
	}
	
	function get_post($post_id, $smileyurl=null)
	{
		$this->load->helper('date');
		if($smileyurl == null){
			$smileyurl = base_url().'static/smileys/';
		}
		$this->db->where('id', $post_id);
		
		$data = $this->db->get('blog_posts');
		
		if( $data->num_rows() !== 0 )
		{
			$post = $data->row_array();
			$post['human_timestamp'] = unix_to_human($post['timestamp']);
			$post['content'] = parse_smileys($post['content'], $smileyurl);
			$post['content'] = $this->functions->fancy_characters($post['content']);
			$post['comments'] = $this->mdl_blog_frontend->get_comments($post['id']);
			return $post;
		}
		else
		{
			return FALSE;
		}
	}
	
	function add_comment($postid, $name, $email, $title, $message){
		$data = array(
			'postid' => $postid ,
			'author' => $name ,
			'email' => $email,
			'title' => $title,
			'message' => $message,
			'timestamp' => date('U'),
			'gravatar' => md5( strtolower( trim($email) ) )
		);

		$this->db->insert('blog_comments', $data); 
	}
}
